<template>
  <div>

    <el-dialog title="采购申请" :visible.sync="dialogBuyReqTableVisible" width="1200px" append-to-body>

      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="单据编号" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入单据编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="需求日期" prop="reqDate">
          <el-date-picker clearable
                          v-model="queryParams.reqDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择需求日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
                :data="buyReqList"
                @selection-change="handleSelectionChange"
        highlight-current-row
        @current-change="handleCurrentChange"
      >
<!--        <el-table-column type="selection" width="55" align="center"/>-->
        <!--      <el-table-column label="主键id" align="center" prop="id"/>-->
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="单据编号" width="180" align="center" fixed>
          <template slot-scope="scope">
            <el-button type="text" @click="handleView(scope.row)">{{ scope.row.code }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="需求人" align="center" prop="reqUserName"/>
        <el-table-column width="180" label="需求部门" align="center" prop="reqUserDept"/>
        <el-table-column label="需求日期" align="center" prop="reqDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.reqDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="单据阶段" align="center" prop="docStage">
          <template slot-scope="scope">
          <span v-for="doc in docStageArr">
            <template v-if="doc.value == scope.row.docStage">
                {{ doc.label }}
            </template>
            <template v-else></template>
          </span>
          </template>
        </el-table-column>
        <el-table-column label="审批结果" align="center" prop="approvalResult">
          <template slot-scope="scope">
          <span v-for="doc in approvalResultArr">
            <template v-if="doc.value == scope.row.approvalResult">
                {{ doc.label }}
            </template>
            <template v-else></template>
          </span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogBuyReqTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listBuyReq} from "@/api/caigou/buyReq";

export default {
  name: "BuyReqSelect",
  props: ['buyReqArr'],
  data() {
    return {
      currentRow: {},
      // 总条数
      total: 0,
      // 显示搜索条件
      showSearch: true,
      dialogBuyReqTableVisible: false,
      // 遮罩层
      loading: true,
      buyReqList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        reqUserId: null,
        reqDate: null,
        docStage: null,
        approvalResult: null,
        isEffect: null,
        isClose: null,
        isCancel: null,
        createBy: null,
        createDate: null,
        modifyBy: null,
        modifyDate: null,
        isDel: null
      },

    }
  },

  methods: {
    // 获取当前行
    handleCurrentChange(val){
      // this.currentRow = val;

      this.buyReqArr[0] = val;
    },

    sure() {
      this.$emit('selectBuyReqMethod', this.buyReqArr)
      this.dialogBuyReqTableVisible = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询采购申请列表 */
    getList() {
      this.loading = true;
      listBuyReq(this.queryParams).then(response => {
        this.buyReqList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      console.log(this.ids)
      console.log(selection)
      this.buyReqArr = selection;
    },
    addSelect() {
      this.dialogBuyReqTableVisible = true;
      this.buyReqList = [];
      this.ids = [];

      this.getList();
    }
  },
  mounted() {
    this.getList();
  }
}
</script>

<style scoped lang="css">

</style>
